import { Strategy, StrategyOptions, ExtractJwt } from 'passport-jwt';
import { PassportStrategy } from '@nestjs/passport';
import { InjectModel } from 'nestjs-typegoose';
import { ReturnModelType } from '@typegoose/typegoose';
import { User } from '@lib/db/models/user.model';


export class JwtStrategy extends PassportStrategy(Strategy) {
  constructor(
    @InjectModel(User) private userModel: ReturnModelType<typeof User>,
  ) {
    super({
      jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
      secretOrKey: '123456aa'
    } as StrategyOptions);
  }

  async validate(id) {
    return await this.userModel.findById(id).populate('roles').populate('permission').populate({
      path: 'roles',
      populate: {
        path: 'permission'
      }
    }).exec()
  }
}
